/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import model.dao.GioHangDAO;
import model.dao.SanPhamDAO;
import model.pojo.ChiTietGioHang;
import model.pojo.ChiTietGioHangId;
import model.pojo.KhachHang;
import model.pojo.SanPham;

/**
 *
 * @author Suma
 */
@WebServlet(name = "GioHang", urlPatterns = {"/GioHang.do"})
public class GioHang extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        SanPhamDAO spdao = new SanPhamDAO();
        String view = "index.jsp";
        double tongtien = 0;
        ArrayList<SanPham> gioHang;
        session = request.getSession();
        if (session.getAttribute("GioHang") == null) {
            gioHang = new ArrayList<SanPham>();
            session.setAttribute("GioHang", gioHang);
        } else {
            gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");
            for (int i = 0; i < gioHang.size(); i++) {
                SanPham s = gioHang.get(i);
                tongtien = tongtien + s.getDonGia() * s.getSoLuong();
            }
        }
        String action = request.getParameter("Action");
        if (action != null) {
            if (action.equals("DatMua")) {
                String maSanPham = request.getParameter("MaSanPham");
                int soluong = Integer.parseInt(request.getParameter("SoLuong"));
                boolean kq = false;
                for (int i = 0; i < gioHang.size(); i++) {
                    SanPham s = gioHang.get(i);
                    if (s.getMaSanPham().equals(maSanPham)) {
                        s.setSoLuong(s.getSoLuong() + soluong);
                        kq = true;
                        break;
                    }
                }

                if (kq == false) {
                    SanPham s = spdao.get(maSanPham);
                    s.setSoLuong(soluong);
                    gioHang.add(s);
                }
                session.setAttribute("GioHang", gioHang);

            } else {
                if (action.equals("Xoa")) {
                    String maSanPham = request.getParameter("MaSanPham");
                    for (int i = 0; i < gioHang.size(); i++) {
                        SanPham s = gioHang.get(i);
                        if (s.getMaSanPham().equals(maSanPham)) {
                            gioHang.remove(i);
                        }
                    }
                } else {
                    if (action.equals("XoaGioHang")) {
                        session.removeAttribute("GioHang");
                        gioHang = new ArrayList<SanPham>();
                        session.setAttribute("GioHang", gioHang);
                    } else {
                        if (action.equals("CheckOut")) {
                            if (session.getAttribute("KhachHang") != null) {
                                GioHangDAO ghdao = new GioHangDAO();
                                KhachHang kh = (KhachHang) session.getAttribute("KhachHang");
                                model.pojo.GioHang ddh = new model.pojo.GioHang();
                                ddh.setMaGioHang(GioHangDAO.sinhMaGioHang());
                                ddh.setNgayTao(new Date());
                                ddh.setKhachHang(kh);

                                ddh.setTongTien(tongtien);
                                ddh.setTinhTrang(0);
                                // JOptionPane.showMessageDialog(null, ddh.getMaDonDatHang());
                                for (int i = 0; i < gioHang.size(); i++) {
                                    Set cthd = new HashSet(1);
                                    SanPham s = gioHang.get(i);
                                    ChiTietGioHangId ctid = new ChiTietGioHangId(ddh.getMaGioHang(), s.getMaSanPham());
                                    ChiTietGioHang ct = new ChiTietGioHang(ctid, ddh, s, s.getDonGia(), s.getSoLuong());
                                    cthd.add(ct);
                                    ddh.setChiTietGioHangs(cthd);
                                    ghdao.saveOrUpdate(ddh);

                                }
                                session.removeAttribute("GioHang");
                                //JOptionPane.showMessageDialog(null, "Thanh toán xong");
                                view = "TrangChu.do";
                                RequestDispatcher rd = request.getRequestDispatcher(view);
                                rd.forward(request, response);
                                return;
                            } else {
                                
                                request.setAttribute("Action", "Nap");
                                request.setAttribute("page", "CheckOut");
                                RequestDispatcher rd = request.getRequestDispatcher(view);
                                rd.forward(request, response);
                                return;
                            }
                        }
                    }
                }
            }
        } else {
            String capNhat = request.getParameter("CapNhat.x");
            if (capNhat != null) {
                String[] s = request.getParameterValues("soLuong");
                for (int i = 0; i < gioHang.size(); i++) {
                    int soLuong = Integer.parseInt(s[i]);
                    gioHang.get(i).setSoLuong(soLuong);
                }
                session.setAttribute("GioHang", gioHang);
            }
        }
        request.setAttribute("page", "GioHang");
        RequestDispatcher rd = request.getRequestDispatcher(view);
        rd.forward(request, response);

    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
